<template>
  <div class="employee-management">
    <h1>员工管理</h1>
    <el-input v-model="searchUsername" placeholder="输入用户名" style="width: 200px; margin-right: 10px;" clearable></el-input>
    <el-select v-model="searchRoleId" placeholder="选择角色" style="width: 200px; margin-right: 10px;" clearable>
      <el-option v-for="role in roles" :key="role.id" :label="role.name" :value="role.id" ></el-option>
    </el-select>
    <el-button type="primary" @click="searchEmployees" >查询</el-button>
    <div class="add-button-container">
      <el-button type="primary" @click="showAddEmployeeDialog">添加员工</el-button>
    </div>
    <el-table :data="employees" style="width: 100%">
      <el-table-column prop="id" label="员工ID" width="180"></el-table-column>
      <el-table-column prop="username" label="用户名" width="180"></el-table-column>
      <el-table-column prop="realName" label="真实姓名" width="180"></el-table-column>
      <el-table-column prop="phone" label="电话" width="180"></el-table-column>
      <el-table-column prop="roleName" label="角色" width="180"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="showEditEmployeeDialog(scope.row)">编辑</el-button>
          <el-button @click="deleteEmployee(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加员工" :visible.sync="addEmployeeDialogVisible">
      <el-form :model="newEmployee">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="newEmployee.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="newEmployee.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="newEmployee.realName"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="newEmployee.phone"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="newEmployee.roleId" placeholder="选择角色">
            <el-option v-for="role in roles" :key="role.id" :label="role.name" :value="role.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addEmployeeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addEmployee">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="编辑员工" :visible.sync="editEmployeeDialogVisible">
      <el-form :model="editEmployee">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editEmployee.username"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="editEmployee.realName"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="editEmployee.phone"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="phone">
          <el-input v-model="editEmployee.password"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="editEmployee.roleId" placeholder="选择角色">
            <el-option v-for="role in roles" :key="role.id" :label="role.name" :value="role.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="editEmployeeDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="updateEmployee">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</template>

<script>
import {getEmployeeList, addEmployee, updateEmployee, deleteEmployee, searchEmployee} from '@/api/employee';
import { getRoleList } from '@/api/role'; // 假设有一个获取角色列表的 API

export default {
  data() {
    return {
      employees: [],
      searchUsername: '', // 查询用户名
      searchRoleId: null, // 查询角色 ID
      roles: [], // 角色列表
      addEmployeeDialogVisible: false,
      editEmployeeDialogVisible: false,
      newEmployee: {
        username: '',
        password: '', // 新增密码字段
        realName: '',
        phone: '',
        roleId: null, // 新增角色 ID
      },
      editEmployee: {},
    };
  },
  methods: {
    async searchEmployees() {
      // 根据用户名和角色 ID 查询员工
      const response = await searchEmployee({ username: this.searchUsername, roleId: this.searchRoleId });
      this.employees = response.data;
    },
    async fetchEmployees() {
      const response = await getEmployeeList();
      this.employees = response.data;
    },
    async fetchRoles() {
      const response = await getRoleList(); // 获取角色列表
      this.roles = response.data;
    },
    showAddEmployeeDialog() {
      this.newEmployee = { username: '', password: '', realName: '', phone: '', roleId: null };
      this.addEmployeeDialogVisible = true;
    },
    async addEmployee() {
      await addEmployee(this.newEmployee);
      this.addEmployeeDialogVisible = false;
      this.fetchEmployees();
    },
    showEditEmployeeDialog(employee) {
      this.editEmployee = { ...employee };
      this.editEmployeeDialogVisible = true;
    },
    async updateEmployee() {
      await updateEmployee(this.editEmployee.id, this.editEmployee);
      this.editEmployeeDialogVisible = false;
      this.fetchEmployees();
    },
    async deleteEmployee(id) {
      await deleteEmployee(id);
      this.fetchEmployees();
    },
  },
  mounted() {
    this.fetchEmployees();
    this.fetchRoles(); // 加载角色列表
  },
};
</script>

<style scoped>
.employee-management {
  padding: 20px;
}
.add-button-container {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}
</style>